<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace Plugin\Alarm\Repository;

use Plugin\Alarm\Model\RdAlarmType;
use Hyperf\Database\Model\Builder;
use App\Repository\IRepository;
use Hyperf\Collection\Arr;

/**
 * 报警消息类型 Repository类
 */
class RdAlarmTypeRepository extends IRepository
{
   public function __construct(
        protected readonly RdAlarmType $model
    ) {}

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        
        // 
        if (isset($params['id']) && filled($params['id'])) {
            $query->where('id', '=', $params['id']);
        }

        // 类型
        if (isset($params['type']) && filled($params['type'])) {
            $query->where('type', '=', $params['type']);
        }

        // 消息名
        if (isset($params['name']) && filled($params['name'])) {
            $query->where('name', 'like', '%'.$params['name'].'%');
        }

        // 语音播报ID
        if (isset($params['voice_id']) && filled($params['voice_id'])) {
            $query->where('voice_id', '=', $params['voice_id']);
        }

        // 排序
        if (isset($params['sort']) && filled($params['sort'])) {
            $query->where('sort', '=', $params['sort']);
        }

        // 风险文本
        if (isset($params['risk_text']) && filled($params['risk_text'])) {
            $query->where('risk_text', 'like', '%'.$params['risk_text'].'%');
        }
        
        // 工单整改限时
        if (isset($params['expire_time']) && filled($params['expire_time'])) {
            $query->where('expire_time', '=', $params['expire_time']);
        }

        // 图标
        if (isset($params['icon']) && filled($params['icon'])) {
            $query->where('icon', 'like', '%'.$params['icon'].'%');
        }

        // 创建者
        if (isset($params['created_by']) && filled($params['created_by'])) {
            $query->where('created_by', '=', $params['created_by']);
        }

        // 更新者
        if (isset($params['updated_by']) && filled($params['updated_by'])) {
            $query->where('updated_by', '=', $params['updated_by']);
        }

        // 
        if (isset($params['created_at']) && filled($params['created_at']) && is_array($params['created_at']) && count($params['created_at']) == 2) {
            $query->whereBetween(
                'created_at',
                [ $params['created_at'][0], $params['created_at'][1] ]
            );
        }

        // 
        if (isset($params['updated_at']) && filled($params['updated_at']) && is_array($params['updated_at']) && count($params['updated_at']) == 2) {
            $query->whereBetween(
                'updated_at',
                [ $params['updated_at'][0], $params['updated_at'][1] ]
            );
        }

        return $query;
    }

    public function findById(mixed $id): mixed
    {
        return $this->getQuery()->with('voice')->whereKey($id)->first();
    }
}